package com.cheyunkeji.er.adapter.fastevaluate;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.cheyun.netsalev3.R;
import com.cheyunkeji.er.adapter.MyBaseAdapter;
import com.cheyunkeji.er.bean.UnAvailableChannelBean;
import com.cheyunkeji.er.bean.UserInfo;
import com.cheyunkeji.er.utils.TimeUtils;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Michael on 2017/3/31 14:19.
 */

public class FastEvaluateChannelListAdapter extends MyBaseAdapter {


    private static final int CHANNEL_USER_STATE_APPLYING = 0;
    private static final int CHANNEL_USER_STATE_USING = 1;
    private static final int CHANNEL_USER_STATE_EXPERIENCEING = 2;
    private static final int CHANNEL_USER_STATE_APPLY_REFUSED = 3;
    private static final int CHANNEL_USER_STATE_LOCKED = 4;
    private static final int CHANNEL_USER_STATE_CHEXIAO = 5;

    private ArrayList<UnAvailableChannelBean> unAvailableChannelList;

    public FastEvaluateChannelListAdapter(ArrayList<UserInfo.ChannelListBean> availableChannelList, ArrayList<UnAvailableChannelBean> unAvailableChannelList, Context context) {
        super(availableChannelList, context);
        this.unAvailableChannelList = unAvailableChannelList;
    }

    @Override
    public int getCount() {
        return dataList.size() + unAvailableChannelList.size();
    }

    @Override
    public Object getItem(int position) {

        if (position < dataList.size()) {
            return dataList.get(position);
        } else {
            return unAvailableChannelList.get(position - dataList.size());
        }
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder holder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.er_fast_evaluate_channel_list_item, parent, false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        if (position < dataList.size()) {

            UserInfo.ChannelListBean bean = (UserInfo.ChannelListBean) dataList.get(position);
            holder.tvChannelName.setText(bean.getTitle());
            holder.tvAdministrator.setText(String.format(context.getResources().getString(R.string.administrator), bean.getAdmin_realname()));
            if(!TextUtils.isEmpty(bean.getAdmin_mobile()))
            holder.tvContactNumber.setText(String.format(context.getResources().getString(R.string.contact_number), bean.getAdmin_mobile().replaceFirst(bean.getAdmin_mobile().substring(3, 7), "****")));

            if ( !TextUtils.isEmpty(String.valueOf(bean.getStarttime())) && !TextUtils.isEmpty(String.valueOf(bean.getEndtime()))){
                holder.tvAuthorizeDate.setText(String.format(context.getResources().getString(R.string.authorize_date), TimeUtils.millis2StringWithoutHMS(Long.valueOf(String.valueOf(bean.getStarttime()) + "000")), TimeUtils.millis2StringWithoutHMS(Long.valueOf(String.valueOf(bean.getEndtime()) + "000"))));
            }

            if ((System.currentTimeMillis() / 1000 - bean.getEndtime()) > 0) {
                holder.tvState.setText(context.getString(R.string.channel_outof_date));
            } else if (bean.getLocked() == 1) {
                holder.tvState.setText(context.getString(R.string.channel_locked));
            } else {

                switch (bean.getCm_state()) {

                    case CHANNEL_USER_STATE_APPLYING:
                        holder.tvState.setText(context.getString(R.string.apply_use));
                        break;
                    case CHANNEL_USER_STATE_USING:
                        holder.tvState.setText(context.getString(R.string.official_use));
                        break;
                    case CHANNEL_USER_STATE_EXPERIENCEING:
                        holder.tvState.setText(context.getString(R.string.experienceing));
                        break;
                    case CHANNEL_USER_STATE_APPLY_REFUSED:
                        holder.tvState.setText(context.getString(R.string.appling_refused));
                        break;
                    case CHANNEL_USER_STATE_LOCKED:
                        holder.tvState.setText(context.getString(R.string.state_locked));
                        break;
                    case CHANNEL_USER_STATE_CHEXIAO:
                        holder.tvState.setText(context.getString(R.string.chexiao_stuff));
                        break;

                    default:
                        break;
                }
            }

        } else {

            UnAvailableChannelBean unAvailableChannelBean = unAvailableChannelList.get(position - dataList.size()) ;
            holder.tvState.setText(unAvailableChannelBean.getState_name());
            holder.tvAdministrator.setText(String.format(context.getResources().getString(R.string.administrator), unAvailableChannelBean.getAdmin_realname()));
            if(!TextUtils.isEmpty(unAvailableChannelBean.getAdmin_mobile()))
            holder.tvContactNumber.setText(String.format(context.getResources().getString(R.string.contact_number), unAvailableChannelBean.getAdmin_mobile().replaceFirst(unAvailableChannelBean.getAdmin_mobile().substring(3, 7), "****")));
            holder.tvChannelName.setText(unAvailableChannelBean.getChannel_name());
            holder.tvAuthorizeDate.setText("");

        }


        return convertView;

    }

    static class ViewHolder {

        @BindView(R.id.tv_state)
        TextView tvState;
        @BindView(R.id.tv_channel_name)
        TextView tvChannelName;
        @BindView(R.id.tv_authorize_date)
        TextView tvAuthorizeDate;
        @BindView(R.id.tv_administrator)
        TextView tvAdministrator;
        @BindView(R.id.tv_contact_number)
        TextView tvContactNumber;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
